package com.feng.chat.config;

import com.feng.chat.interceptor.LoginInterceptor;
import com.feng.chat.interceptor.MessageInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import javax.servlet.Filter;

@Configuration
public class MyConfig extends WebMvcConfigurationSupport {
    @Autowired
    private LoginInterceptor loginInterceptor;

    @Autowired
    private MessageInterceptor messageInterceptor;

    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

    /**
     * 文件资源映射
     * @param registry
     */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("file:E:\\Desktop\\案例\\javaweb案例\\MyChat\\resource\\");
    }


    /**
     * 注册拦截器
     * @param registry
     */
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        //添加登录拦截器
        registry.addInterceptor(loginInterceptor).addPathPatterns("/api/v1/**").excludePathPatterns("/api/v1/auth/**","");
        //添加消息权限拦截器
        registry.addInterceptor(messageInterceptor).addPathPatterns("/api/v1/talk/message/**").excludePathPatterns("/api/v1/talk/message/forward");
    }
}
